package com.lanvideo.dao;

import com.lanvideo.entity.ExamQuestion;
import com.lanvideo.entity.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 考试题目关联DAO接口
 */
@Repository
public class ExamQuestionDao {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 添加题目到考试
     */
    public int addQuestionToExam(Integer examId, Integer questionId) {
        // 先检查是否已存在
        String checkSql = "SELECT COUNT(*) FROM exam_question WHERE exam_id=? AND question_id=?";
        Integer count = jdbcTemplate.queryForObject(checkSql, Integer.class, examId, questionId);
        
        if (count != null && count > 0) {
            return 0; // 已存在，返回0表示没有新增
        }
        
        String sql = "INSERT INTO exam_question (exam_id, question_id) VALUES (?, ?)";
        return jdbcTemplate.update(sql, examId, questionId);
    }

    /**
     * 从考试中移除题目
     */
    public int removeQuestionFromExam(Integer examId, Integer questionId) {
        String sql = "DELETE FROM exam_question WHERE exam_id=? AND question_id=?";
        return jdbcTemplate.update(sql, examId, questionId);
    }

    /**
     * 获取考试的所有题目
     */
    public List<ExamQuestion> getExamQuestions(Integer examId) {
        try {
            String sql = "SELECT eq.*, q.content, q.option_a, q.option_b, q.option_c, q.option_d, q.correct_ans " +
                    "FROM exam_question eq " +
                    "INNER JOIN question_bank q ON eq.question_id = q.id " +
                    "WHERE eq.exam_id=? ORDER BY eq.id";
            
            
            List<ExamQuestion> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<ExamQuestion>() {
                @Override
                public ExamQuestion mapRow(java.sql.ResultSet rs, int rowNum) throws java.sql.SQLException {
                    ExamQuestion examQuestion = new ExamQuestion();
                    examQuestion.setId(rs.getInt("id"));
                    examQuestion.setExamId(rs.getInt("exam_id"));
                    examQuestion.setQuestionId(rs.getInt("question_id"));
                    examQuestion.setQuestionOrder(rowNum); // 使用行号作为顺序
                    
                    Question question = new Question();
                    question.setId(rs.getInt("question_id"));
                    question.setContent(rs.getString("content"));
                    question.setOptionA(rs.getString("option_a"));
                    question.setOptionB(rs.getString("option_b"));
                    question.setOptionC(rs.getString("option_c"));
                    question.setOptionD(rs.getString("option_d"));
                    question.setCorrectAns(rs.getString("correct_ans"));
                    // 设置默认值
                    question.setDifficulty(1);
                    question.setCategory("默认分类");
                    question.setTeacherId(1);
                    
                    examQuestion.setQuestion(question);
                    return examQuestion;
                }
            }, examId);
            
            return result;
            
        } catch (Exception e) {
            throw new RuntimeException("获取考试题目失败: " + e.getMessage(), e);
        }
    }

    /**
     * 获取考试题目数量
     */
    public int getExamQuestionCount(Integer examId) {
        String sql = "SELECT COUNT(*) FROM exam_question WHERE exam_id=?";
        return jdbcTemplate.queryForObject(sql, Integer.class, examId);
    }

    /**
     * 清空考试的所有题目
     */
    public int clearExamQuestions(Integer examId) {
        String sql = "DELETE FROM exam_question WHERE exam_id=?";
        return jdbcTemplate.update(sql, examId);
    }

    /**
     * 添加考试题目
     */
    public int addExamQuestion(ExamQuestion examQuestion) {
        String sql = "INSERT INTO exam_question (exam_id, question_id) VALUES (?, ?)";
        return jdbcTemplate.update(sql, examQuestion.getExamId(), examQuestion.getQuestionId());
    }
}
